package cn.honglian.org.controller;

import cn.honglian.basic.dto.ShopAuditLogDto;
import cn.honglian.basic.dto.ShopSettlementDto;
import cn.honglian.basic.util.AjaxResult;
import cn.honglian.basic.util.ExcelUtil;
import cn.honglian.basic.util.PageList;
import cn.honglian.basic.vo.ShopLogVo;
import cn.honglian.basic.vo.ShopVo;
import cn.honglian.org.domain.Employee;
import cn.honglian.org.domain.Shop;
import cn.honglian.org.domain.ShopAuditLog;
import cn.honglian.org.query.ShopQuery;
import cn.honglian.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    @PatchMapping
    public AjaxResult patch(@RequestBody List<Long> ids){

            shopService.batchDelete(ids);
            return AjaxResult.me().success();

    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
//    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryByPage(query);
    }

    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody @Valid ShopSettlementDto shopSettlementDto)
    {

        shopService.settlement(shopSettlementDto);
        return AjaxResult.me().success();
    }
    @PostMapping("/settlement/again")
    public AjaxResult again(@RequestBody @Valid ShopSettlementDto shopSettlementDto){

        shopService.settlementAgain(shopSettlementDto);
        return AjaxResult.me().success();
    }

    @PostMapping("/audit/passandreject")
    public AjaxResult passAndreject(@RequestBody @Valid ShopAuditLogDto shopAuditLogDto) {


        try {
            if (0==shopAuditLogDto.getType()){
                shopService.auditReject(shopAuditLogDto);
            } else {
                shopService.auditPass(shopAuditLogDto);
            }
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        return AjaxResult.me().success();
    }

    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse res){
        List<Shop> shops = shopService.findAll();
        ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"店铺信息.xls",res);

    }

    @PostMapping("/import/excel")
    public void importExcel(@RequestPart("file")    MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);

        shopService.addAll(shops);
    }

    /**
     * 点击展开行 获取这一行的日志
     *
     * @param
     * @return
     */
    @PostMapping("/list")
    public PageList<ShopLogVo> expandChange(@RequestBody ShopQuery query){
        return shopService.expandChange(query);

    }




}
